package com.loong.loong.hik.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.loong.loong.hik.config.HikConfig;
import com.loong.loong.hik.property.request.resource.ResourcePersonPictureRequest;
import com.loong.loong.hik.property.result.resource.ResourcePersonPictureResult;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;


/**
 * 海康调用接口工具类
 *
 * @author jinyiang
 * @date 2023/3/10 17:40
 */
@Service
public class HikUtil {


	/**
	 * 海康post接口
	 *
	 * @param url
	 * @param inparmT  入参实体类
	 * @param outparmT 出参实体类
	 * @return
	 */
	public <T> T doPost(String url, Object inparmT, Class<T> outparmT) {
		try {
			ArtemisConfig config = new ArtemisConfig();
			config.setHost(HikConfig.HOST); // 代理API网关nginx服务器ip端口
			config.setAppKey(HikConfig.APP_KEY);  // 秘钥appkey
			config.setAppSecret(HikConfig.APP_SECRET);// 秘钥appSecret


			String body = "";
			if (outparmT != null) {
				body = JSON.toJSON(inparmT).toString();
			} else {
				body = "";
			}

			Map<String, String> path = new HashMap<String, String>(2) {
				{
					put("https://", url);
				}
			};

			String result = ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");

			if (result != null) {
				JSONObject outParm = JSONObject.parseObject(result);
				if ("0".equals(outParm.getString("code"))) {
					return (T) JSONObject.toJavaObject(outParm, outparmT.newInstance().getClass());
				}
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 海康post接口
	 * @param url
	 * @param resourcePersonPictureRequest
	 * @param resourcePersonPictureResult
	 * @return
	 */
	public ResourcePersonPictureResult doPostImg(String url, ResourcePersonPictureRequest resourcePersonPictureRequest, ResourcePersonPictureResult resourcePersonPictureResult) {
		ArtemisConfig config = new ArtemisConfig();
		config.setHost(HikConfig.HOST); // 代理API网关nginx服务器ip端口
		config.setAppKey(HikConfig.APP_KEY);  // 秘钥appkey
		config.setAppSecret(HikConfig.APP_SECRET);// 秘钥appSecret

		String body = JSON.toJSON(resourcePersonPictureRequest).toString();
		Map<String, String> path = new HashMap<String, String>(2) {
			{
				put("https://", url);
			}
		};
		Map<String, String> head = new HashMap<String, String>(2) {  //get请求的head参数
			{
				put("headpost", "sky-test");
			}
		};
		Map<String, String> query = new HashMap<String, String>(2) {  //get请求的head参数
			{
				put("domainId", "0");
			}
		};
		try {
			HttpResponse resp = ArtemisHttpUtil.doPostStringImgArtemis(config, path, body, query, null, "application/json", head);
			if (302 == resp.getStatusLine().getStatusCode()) {
                /*
                获取图片数据保存到本地
                注：1.对于有时效的图片，必须尽快保存到本地
                   2.若无时效，则可以直接保存location，后续自行访问获取
                 */
				Header header = resp.getFirstHeader("location");
				String newUrl = header.getValue();
				resourcePersonPictureResult.setUrl(newUrl);
//				HttpGet httpget = new HttpGet(newUrl);
//				HttpClient httpClient = wrapClient(httpget.getURI().getScheme() + "://" + httpget.getURI().getHost());
//				HttpResponse execute = httpClient.execute(httpget);
//				HttpEntity entity = execute.getEntity();
//				InputStream in = entity.getContent();
//				Tools.savePicToDisk(in, HikConfig.DOWN_PATH, "test311.jpg");
			} else {
				System.out.println("下载出错");
				resourcePersonPictureResult.setMsg(resp.getStatusLine().getStatusCode()+"");
			}
			return resourcePersonPictureResult;
		} catch (Exception e) {
			e.printStackTrace();
			resourcePersonPictureResult.setCode("1003");
			resourcePersonPictureResult.setMsg(e.toString());
		}
		return resourcePersonPictureResult;
	}

}
